package com.example.ka;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class kaoqin extends Activity {
    private ListView listview;
    private ArrayList<Map<String, Object>> data;
    private SQLiteDatabase db;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kaoqin);
        listview = findViewById(R.id.list_book);
        String DB_NAME = "mydb";
        com.example.ka.Mysql Mysql = new Mysql(this,DB_NAME,null,1);
        db = Mysql.getWritableDatabase();// 打开数据库
        data = new ArrayList<>();
        dbFindAll();
    }
    //查询数据
    protected void dbFindAll() {
        // TODO Auto-generated method stub
        data.clear();
        Cursor cursor = db.query("account", null, null, null, null, null, "_id ASC");
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            String id = cursor.getString(1);
            String name = cursor.getString(2);
            String beginDate = cursor.getString(3);
            String endDate = cursor.getString(4);
            Map<String, Object> item = new HashMap<>();
            item.put("id", id);
            item.put("name", name);
            item.put("beginDate", beginDate);
            item.put("endDate", endDate);
            data.add(item);
            cursor.moveToNext();
        }
        cursor.close();
        showList();
    }
    private void showList() {
        // TODO Auto-generated method stub
        SimpleAdapter listAdapter = new SimpleAdapter(this, data,
                R.layout.list_item, new String[]{"id", "name", "begindata", "enddate"}, new int[]{R.id.tvNo, R.id.tvName, R.id.tvAr, R.id.tvPr,});
        listview.setAdapter(listAdapter);
    }

}
